package com.wang.jmonkey.modules.system.model.param;

import cn.hutool.core.collection.CollUtil;
import com.wang.jmonkey.modules.system.model.entity.SystemAclEntity;
import com.wang.jmonkey.modules.system.model.enums.SystemAclPrincipalTypeEnum;
import lombok.Data;
import lombok.experimental.Accessors;

import javax.validation.constraints.NotNull;
import java.util.List;

/**
 * @Description 资源授权参数
 * @Author HeJiawang
 * @Date 2021/5/22 11:24
 */
@Data
@Accessors(chain = true)
public class SystemAclAuthParam {

    /**
     * 权限主体类型 user 代表system_user ; role 代表system_role
     */
    private SystemAclPrincipalTypeEnum principalType;

    /**
     * 权限主体id，如 principal_type = user 则 principal_id = system_user.id
     */
    @NotNull(message = "授权主体id不能为空")
    private Long principalId;

    /**
     * 资源id集合
     */
    private List<Long> resourceIdList;

    /**
     * convertToAclEntity
     * @return List<SystemAclEntity>
     */
    public List<SystemAclEntity> convertToAclList() {
        List<SystemAclEntity> result = CollUtil.newArrayList();
        if (CollUtil.isEmpty(this.resourceIdList)) return result;

        resourceIdList.forEach( resourceId -> {
            result.add(
                    new SystemAclEntity().setPrincipalType(this.principalType)
                            .setPrincipalId(this.principalId)
                            .setResourceId(resourceId)
            );
        });

        return result;
    }
}
